//
//  SEGDoorPresentAnimation.m
//  SEGIntelligentHardware
//
//  Created by 梁学彰 on 2020/11/26.
//

#import "SEGDoorPresentAnimation.h"

@implementation SEGDoorPresentAnimation
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.3f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    UIView* toView = nil;
    UIView* fromView = nil;
    UIView* transView = nil;
    
    if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
        fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    } else {
        fromView = fromViewController.view;
        toView = toViewController.view;
    }
    
    if (_isPresent) {
        UIView *maskView = [[UIView alloc] initWithFrame:toView.bounds];
        maskView.tag = 9898;
        maskView.backgroundColor = [UIColor clearColor];
        transView = toView;
        [[transitionContext containerView] addSubview:maskView];
        [maskView addSubview:toView];
        
    }else {
        UIView *maskView = [[transitionContext containerView] viewWithTag:9898];
        transView = fromView;
        if (maskView) {
            [maskView insertSubview:toView belowSubview:fromView];
        }else {
            [[transitionContext containerView] insertSubview:toView belowSubview:fromView];
        }
    }
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    UIView *maskView = [[transitionContext containerView] viewWithTag:9898];
    transView.frame = CGRectMake(0, _isPresent ?height :0, width, height);
    if (!_isPresent) {
        transView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        transView.frame = CGRectMake(0, self.isPresent ?0 :height, width, height);
        if (self.isPresent) {
            maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:self->_isPresent?0.5:0];
        }else {
            maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:self->_isPresent?0.5:0];
        }
    } completion:^(BOOL finished) {
        if (self.isPresent) {
            maskView.backgroundColor = [UIColor clearColor];
            transView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        }
        [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
    }];
    
}
@end
